home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / VW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  4.4 KB  |  160 lines

  1. // vw.cpp: Implements a scrollable window with a virtual buffer 
  2.  
  3. #include <math.h>
  4. #include "txscroll.h"
  5.  
  6. // A specific type of Swso scroll window: a virtual window 
  7.  
  8. class Vwso : public Swso {
  9. public:
  10.   Trso *VirtBuff;
  11.   int Vx, Vy;
  12.   // Correction: Pg 280, Cp passed by reference, not by
  13.   //             pointer as shown in the book
  14.   Vwso(int Fa, ColorPak &Cp);
  15.   virtual ~Vwso(void);
  16.   virtual void Draw(void);
  17.   virtual void Open(Iso *B, int X, int Y);
  18.   virtual void OnKeyStroke(MsgPkt &M);
  19.   virtual void SendScrollPosn(void);
  20.   virtual void RcvScrollPosn(float P, BarOrient Which);
  21. };
  22.  
  23. // ---------------------- Vwso Methods ---------------------- 
  24.  
  25. // Correction: pg 281, Cp passed by reference, not by
  26. //             by pointer as in book
  27. Vwso::Vwso(int Fa, ColorPak &Cp)
  28. // Initialize Vwso object and its virtual buffer 
  29. : Swso(Fa, Cp)
  30. {
  31.   Vx = 0;  Vy = 0;
  32.   VirtBuff = new Trso(NULL);
  33.   VirtBuff->SetSize(100, 100); // Hard-coded as 100x100 characters
  34. }
  35.  
  36. Vwso::~Vwso(void)
  37. // Free virtual buffer 
  38. {
  39.   delete VirtBuff;
  40. }
  41.  
  42. void Vwso::Draw(void)
  43. // Put a portion of the virtual buffer up on the screen 
  44. {
  45.   Trso *Tp;
  46.  
  47.   Swso::Draw();
  48.   Tp = (Trso *)(Panel->Interior);
  49.   // Copy from virtual buffer at (Vx, Vy) to the interior
  50.   Tp->Xfr(0, 0, Tp->Wd, Tp->Ht, VirtBuff, Vx, Vy);
  51. }
  52.  
  53.  
  54. char Line1[] = "0123456789012345678901234567890123456789";
  55. char Line2[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
  56. char Line3[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
  57. char Last[]  = "Bottom-Right of virtual buffer";
  58.  
  59. void Vwso::Open(Iso *B, int X, int Y) 
  60. // First, fill virtual buffer with some stuff, open up 
  61. {
  62.   // Clear virtual buffer with a red graphics fill character 
  63.   VirtBuff->Fill(0, 0, VirtBuff->Wd, VirtBuff->Ht, 176, 0x04);
  64.   // Then draw some lines
  65.   VirtBuff->HzWrt(0, 0, Line1, 0x70);
  66.   VirtBuff->HzWrt(0, 1, Line2, 0x70);
  67.   VirtBuff->HzWrt(0, 2, Line3, 0x70);
  68.   VirtBuff->HzWrt(70, 99, Last, 0x70);
  69.   // And some boxes
  70.   VirtBuff->Box(5, 5, 50, 5, 0x11, 0x70);  
  71.   VirtBuff->Box(70, 50, 25, 3, 0x11, 0x73);
  72.   VirtBuff->Box(10, 30, 20, 5, 0x11, 0x74);
  73.   // Now open up the virtual window 
  74.   Swso::Open(B, X, Y);
  75. }
  76.  
  77. void Vwso::OnKeyStroke(MsgPkt &M)
  78. // Keypresses are passed to the Vwso object. Handle them here. Depending
  79. // on which arrow key is pressed, update the offsets, Vx and Vy, into
  80. // the virtual buffer which determines what will be displayed in the
  81. // scrollable window. Call Draw to update the window and finally
  82. // SendScrollPosn to send a message to scroll bars indicating the
  83. // new location in the virutal buffer. 
  84. {
  85.   switch(M.Code) {
  86.     case UpKey: 
  87.       if (Vy > 0) Vy--;
  88.       Draw();
  89.       SendScrollPosn();   // Update bar position 
  90.     break;
  91.     case DownKey: 
  92.       if (Vy < (VirtBuff->Ht - Panel->Interior->Ht)) Vy++;
  93.       Draw();
  94.       SendScrollPosn();
  95.     break;
  96.     case LeftKey:
  97.       if (Vx > 0) Vx--;
  98.       Draw();
  99.       SendScrollPosn();
  100.     break;
  101.     case RightKey: 
  102.       if (Vx < (VirtBuff->Wd - Panel->Interior->Wd)) Vx++;
  103.       Draw();
  104.       SendScrollPosn();
  105.     break;
  106.     default:
  107.       Swso::OnKeyStroke(M);  // Let parent class handle it
  108.   }
  109. }
  110.  
  111. void Vwso::SendScrollPosn(void)
  112. // Send messages to both scroll bars indicating current offset into
  113. // virtual buffer 
  114. {
  115.   float P; 
  116.   int W;
  117.  
  118.   W = VirtBuff->Wd-Panel->Interior->Wd;
  119.   if (W == 0) P = 0.0; else P = float(Vx) / float(W);
  120.   HzSlide->RcvScrollPosn(P, HzOrient);
  121.  
  122.   W = VirtBuff->Ht-Panel->Interior->Ht;
  123.   if (W == 0) P = 0.0; else P = float(Vy) / float(W);
  124.   VtSlide->RcvScrollPosn(P, VtOrient);
  125. }
  126.  
  127. void Vwso::RcvScrollPosn(float P, BarOrient Which)
  128. // Update the current offset into the virtual buffer, based on the
  129. // value P. Redraw the display window with this new setting. 
  130. {
  131.   int W;
  132.   if (Which == HzOrient) {
  133.      W = VirtBuff->Wd - Panel->Interior->Wd;
  134.      Vx = ceil(P*W);
  135.   }
  136.   else {
  137.      W = VirtBuff->Ht - Panel->Interior->Ht;
  138.      Vy = ceil(P*W);
  139.   }
  140.   Draw();
  141. }
  142.  
  143. Wso *W;
  144.  
  145. main()
  146. {
  147.   // CORRECTION: pg 283, book passed DefColors by pointer, 
  148.   // instead of by reference as it should have
  149.   Setup(MouseOptional, DefColors);
  150.   FullScrn->Panel->Clear(176, 0x07);
  151.   // CORRECTION: pg 283, book pased CyanColors by pointer,
  152.   // instead of by reference as it should have
  153.   W = new Vwso(WindowStyle+Stretchable, CyanColors);
  154.   W->SetSize(30, 10);
  155.   W->Open(FullScrn, 5, 5);
  156.   MainEventLoop();
  157.   CleanUp();
  158. }
  159.  
  160.